package com.frxuexi.shop2307.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.room.YueEntity
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.KillAdapter
import com.frxuexi.shop2307.adapter.YueAdapter
import com.frxuexi.shop2307.databinding.ActivityKillBinding
import com.frxuexi.shop2307.shop.ShopViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

@AndroidEntryPoint
@Route(path = "/app/KillActivity")
class KillActivity : BaseActivity<ActivityKillBinding,ShopViewModel>() {
    val killAdapter by lazy { KillAdapter() }
    val yueAdapter by lazy { YueAdapter() }
    var timer:Timer?=null
    var num=59
    override fun initData() {
        vm.productData.observe(this){
            killAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
            yueAdapter.apply {
                data.clear()
                it.list.forEach { it.isYue=false }
                addData(it.list)
                notifyDataSetChanged()
            }
            vm.selectYue()

        }

        vm.addYueData.observe(this){
            ToastUtils.showLong("预约成功")
            vm.selectYue()
        }

        vm.selectYueData.observe(this){yueList->
            yueAdapter.data.filter {yueList.map { it.sid }.contains(it.id)}.forEach {
                it.isYue=true
            }
            yueAdapter.notifyDataSetChanged()
        }

    }

    override fun initView() {
        lifecycleScope.launch(Dispatchers.IO) {
            timer= Timer()
            timer?.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main) {
                    num--
                    binding.killTime.setText(num.toString())
                    if(num==0){
                        timer?.cancel()
                    }
                }
            },0,1000)
        }

        binding.killRv.apply {
            adapter=killAdapter
            layoutManager=LinearLayoutManager(this@KillActivity)
        }
        vm.getProducts(1,1)


        killAdapter.addChildClickViewIds(R.id.car_qiang)
        killAdapter.setOnItemChildClickListener { adapter, view, position ->
            if(num==0){
                ToastUtils.showLong("秒杀已结束")
            }else{
                ToastUtils.showLong("已抢")
            }
        }

        binding.killCon2.setOnClickListener {
            binding.killRv.adapter=killAdapter

        }

        binding.killCon3.setOnClickListener {
            binding.killRv.adapter=yueAdapter
        }

        yueAdapter.addChildClickViewIds(R.id.car_yue)
        yueAdapter.setOnItemChildClickListener { adapter, view, position ->
            vm.addYue(YueEntity(yueAdapter.data.get(position).id))
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.cancel()
        timer=null
    }

}